package com.extr.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.extr.security.UserInfo;

/**
 *项目：广发网
 *版权：Copyright(c)2013
 *公司：广发证券股份有限公司
 *作者：胡纬
 *创建日期：2015-10-23
 *创建时间：下午5:03:06
 *功能描述：
 */
@Controller
public class MobileController {
    @RequestMapping(value = "/mobile/login", method = RequestMethod.GET)
    public String loginPage(Model model,@RequestParam(value = "result", required = false, defaultValue = "") String result) {
        if("failed".equals(result)){
            model.addAttribute("result", "无效的用户名或者密码");
        }
        return "mobile/login";
    }
    
    @RequestMapping(value = "/mobile/home", method = RequestMethod.GET)
    public String homePage(Model model) {
        try{
            UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
        }catch(Exception ex){
            return "mobile/login";
        }        
        return "mobile/home";
    }
    
    @RequestMapping(value = "/mobile/usercenter", method = RequestMethod.GET)
    public String usercenterPage(Model model) {
        UserInfo userInfo = null;
        try{
             userInfo = (UserInfo) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
        }catch(Exception ex){
            return "mobile/login";
        }
        model.addAttribute("username", userInfo.getUsername());
        model.addAttribute("email",userInfo.getEmail());
        model.addAttribute("lastLoginTime", userInfo.getLastLoginTime());
        model.addAttribute("field",userInfo.getFieldName());
        return "mobile/usercenter";
    }
}
